﻿/* 
 * Copyright (c) 2011 Jeff Zhang
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of 
 * this software and associated documentation files (the "Software"), 
 * to deal in the Software without restriction, including without limitation the rights to use, 
 * copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
 * and to permit persons to whom the Software is furnished to do so, 
 * subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included 
 * in all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 
 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 
 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace Json {
    public class JsonNumber : JsonObject<Number> {
        public JsonNumber() : base(new Number(0)) { }

        public JsonNumber(byte value) : base(new Number(value)) { }

        public JsonNumber(short value) : base(new Number(value)) { }

        public JsonNumber(ushort value) : base(new Number(value)) { }

        public JsonNumber(int value) : base(new Number(value)) { }

        public JsonNumber(uint value) : base(new Number(value)) { }

        public JsonNumber(long value) : base(new Number(value)) { }

        public JsonNumber(ulong value) : base(new Number(value)) { }

        public JsonNumber(float value) : base(new Number(value)) { }

        public JsonNumber(double value) : base(new Number(value)) { }

        public JsonNumber(Number number) : base(number) { }

        public static JsonNumber NaN = new JsonNumber(float.NaN);

        public override int Length {
            get { throw new IllegalOperationException(); }
        }

        public override IJsonObject this[string key] {
            get {
                throw new IllegalOperationException();
            }
            set {
                throw new IllegalOperationException();
            }
        }

        public override IJsonObject this[int index] {
            get {
                throw new IllegalOperationException();
            }
            set {
                throw new IllegalOperationException();
            }
        }

        public override string ToJson() {
            return base.ToJson();
        }
    }

    public class JsonNumberArray : JsonObject<IList<Number>> {
        public JsonNumberArray() : base(new List<Number>()) { }
        public JsonNumberArray(int capacity) : base(new List<Number>(capacity)) { }
        public JsonNumberArray(IEnumerable<Number> list) : base(new List<Number>(list)) { }

        public override int Length {
            get {
                return Value.Count;
            }
        }

        #region add methods

        public void Add(byte value) {
            Value.Add(new Number(value));
        }

        public void Add(short value) {
            Value.Add(new Number(value));
        }

        public void Add(ushort value) {
            Value.Add(new Number(value));
        }

        public void Add(int value) {
            Value.Add(new Number(value));
        }

        public void Add(uint value) {
            Value.Add(new Number(value));
        }

        public void Add(long value) {
            Value.Add(new Number(value));
        }

        public void Add(ulong value) {
            Value.Add(new Number(value));
        }

        public void Add(float value) {
            Value.Add(new Number(value));
        }

        public void Add(double value) {
            Value.Add(new Number(value));
        }

        public void Add(Number value) {
            Value.Add(value);
        }

        #endregion

        public override IJsonObject this[string key] {
            get { throw new IllegalOperationException(); }
            set { throw new IllegalOperationException(); }
        }

        public override IJsonObject this[int index] {
            get { return new JsonNumber(Value[index]); }
            set {
                if (value is JsonNumber) {
                    Value[index] = (Number)value.Value;
                }
            }
        }

        public override string ToJson() {
            StringBuilder sb = new StringBuilder();

            sb.Append("[");

            int len = Value.Count;

            for (int i = 0; i < len; i++) {
                sb.AppendFormat("{0},", Value[i].ToString());
            }

            return sb.ToString().TrimEnd(',') + "]";
        }
    }
}
